< previous page page_375 next page >

Page 375
Table 16.4 Collection Object Built-In Properties and Methods
ItemTask
Count propertyReturns the number of items in the collection
Add methodAdds an object to the collection
Item methodGets a single object from the collection
Remove methodDeletes an object from the collection

The code in Listing 16.2 creates a new collection named colTextBoxes and adds all the text boxes on a form to the new collection.
Listing 16.2 COLLECT.ZIPCreating a Collection with This Code
Option Explicit

' Create a new collection to contain all the
' text boxes on a form
Dim colTextBoxes As New Collection

Private Sub Form_Initialize()
    ' Variable used in For Each to get controls.
    Dim cntrlItem As Control
    ' Loop through the controls on the form.
    For Each cntrlItem In Me.Controls
        ' If the control is a text box, add it to the
        ' collection of text boxes.
        If TypeName(cntrlItem) = "TextBox" Then
            colTextBoxes.Add cntrlItem
        End If
    Next cntrlItem
End Sub
The code in Listing 16.3 uses the collection colTextBoxes to clear all the text entered on the form.
Listing 16.3 Using For Each to Handle the Collection
Sub cmdClear_Click()
    ' Variable used in For Each to get controls.
    Dim cntrlItem As Control
    ' Clear each of the text boxes in the collection.
    For Each cntrlItem In colTextBoxes
        cntrlItem.Text = ""
    Next cntrlItem
End Sub
sarrow.gifSee "Using the ListView Control," p. 251

 
< previous page page_375 next page >